var http = require('http');
var soap = require('soap');

var wsdlFilePath = 'chws.wsdl';
var wsdlFile = require('fs').readFileSync(wsdlFilePath, 'utf8');
var chWebservice = {
  ChWebservice: {
    ChWebservicePort: {
      accountInformation: function(req) {
		console.log('received request: ' + JSON.stringify(req));
        if (req.tickerSymbol === 'trigger error') {
          throw new Error('triggered server error');
        } else {
          return { chTransactionId: '123test', 
					accountInformation: {balanceCurrency: 'USD', name: 'Abdullahi Awale', accountNumber: '1998766555', 
					balance: 109.08, maxBalance: 300.00, state: 'ACTIVE'}
				 };
        }
      }
    }
  }
}

var server = http.createServer(function(req, res) {
  res.end("404: Not Found: "+req.url)
});
server.listen(8080);
server.on('listening',function(){
    console.log('ok, server is running at: ' + server.address().address + ":" + server.address().port);
});

soap.listen(server, '/chwebservice', chWebservice, wsdlFile);